var img_x = 0;
var img_y = 0;
var img_w = 0;
var img_h = 0;
var userlistcache = "";
var interval;
$().ready(function(){
	$(".revert_revert").each(function(){
			$(this).bind("click",function(){return revert(this);});
	});
	
	$('.revertpost').click(function() {
			postRevert(this);
	});
	
	$(".delrevert").each(function(){
			$(this).bind("click",function(){return delRevert(this);});
	});
	$(".deletephoto").click(function(){
		alertMessage(this,"确定删除这张相片");
		var url = $(this).attr("href");
		$(".msgOk").bind("click",function(){
			location.href = url;	
			$("#messageBox").remove();
		});
		return false;
	});
	$(".setdefault").click(function(){
		//alertMessage(this,"确定设置为封存面");
		var url = $(this).attr("href");
		$.get(url,{ajax:1},function(data){
			if("success"==data){
				$(".setdefault").after("成功设置为相册封面").remove();
			}else{
				msg($(".setdefault"),"设置失败");
			}
		});
		return false;
	});

	$("#quanren").click(function(){ //圈人开始
		$("#quanren_end").parent().css("display","block");
		quanren();
	});
	$("#quanren_end").click(function(){ //圈人结束
		//$("#quanrenDiv").remove();
		//$("#firendListDiv").remove();
		$('img#img')
				.imgAreaSelect({ 
					disable : true,
					hide : true
				});
		
		$("#quanren_end").parent().css("display","none");
		$("#img").unbind("click");
		$('#img').css("cursor","pointer");
	});
	$('.nametag').each(function(){
		$(this).bind("mouseover",function() {
			img_x = parseInt($("#img").offset().left); //图片的左距
			img_y = parseInt($("#img").offset().top);//图片的顶距
			img_w = parseInt($("#img").width());//图片的宽
			img_h = parseInt($("#img").height());//图片的高
			var zb = $(this).attr("tag");
			var _x = zb.split(",")[0];
			var _y = zb.split(",")[1];
			if($("#lookDiv").attr("id") == "lookDiv"){
				$("#lookDiv").remove();
			}
			var div_box3 = document.createElement("DIV");
			div_box3.id = "lookDiv";
			div_box3.style.position ="absolute";
			div_box3.style.width = "100px";
			div_box3.style.height = "100px";
			div_box3.style.border = "3px solid #fff";
			div_box3.style.top = (_y*1+img_y*1) +"px";
			div_box3.style.left = (_x*1+img_x*1) + "px";
			document.body.appendChild(div_box3);
		});

		$(this).bind("mouseout",function() {
			$("#lookDiv").remove();
		});
	});
});


function postRevert(obj){
	var reid = $(obj).next().val();
	if(reid==0){
		var content = getContents("content");
	}else{
		var content= getContents("revertcontent");
	}
	if($.trim(content) == ""){
		msg(obj,"内容不能为空");
		return false;
	}else{
		$(obj).attr("disabled", "disabled");
		var photoid = $("#photoid").val();
		$.post(APP+"/photo/savephotorevert", {ajax:1,photoid: photoid, reid: reid , content: content },
		function(data){
			if("error" == data){
				msg("obj","发送失败，你可以重试");
				$(obj).removeAttr("disabled");
			}else{
				show(obj,data,content); 
			}
		});
	}
}

function show(obj,data,content){
	//alert(data);
	if(data == "error"){
		alertMessage(obj,"评论失败");
	}else{
		var s = data.split(",");
		var html = '<div class="revertlist"><div class="revert_username_face">';
		html += '<img src="'+ROOT+'/Public/Uploads/'+s[1]+'/face/small_face.jpg"/></div>';
		html += '<div class="revertcontent"><div class="reverttitle">';
		html += '<a href="'+APP+'/User/Index/u/'+s[1]+'">'+s[2]+'</a> 刚刚 <a href="#" class="delrevert" id="'+s[0]+'">删除</a></div>';
		html += '<div class="revert_content">'+content+'</div>';
		html += '</div></div>';
		if($(".revertcancel").val() == "取消"){
			$(".revertcancel").parent().parent().parent().parent().parent().append(html);
			$(".revertcancel").parent().parent().remove();
		}else{
			$(obj).parent().parent().prev().append(html);
			$(obj).parent().prev().children("#revertcontent").val("");
			setContents("content","");
		}
		$(".delrevert").bind("click",function(){return delRevert(this);});	
	}
	$(obj).removeAttr("disabled");
}

function delRevert(obj){
	alertMessage(obj,"确定删除");
	$(".msgOk").bind("click",function(){delRevertAction(obj);});
	return false;
}

function delRevertAction(obj){
	$.post(APP+"/photo/delphotorevert", {ajax:1,revertid: $(obj).attr("id"),photoid:$("#photoid").val()} , 
	function(data){ 
		if("success" == data){
			$(obj).parent().parent().parent().remove(); 
			$("#messageBox").remove();
		}else{
			msg(obj,"操作失败");
		}
	});
}

function revert(obj){
	var	 html =	'<div><p><textarea id="revertcontent" name="revertcontent" cols="30" rows="6"></textarea>';
	html += '</p><p><input type="hidden" value="'+$("#photoid").val()+'"/>';
	html += '</p><p><input type="hidden" value="'+$("#photoid").next().val()+'"/>';
	html += '<input type="hidden" value="'+$("#photoid").next().next().val()+'"/>';
	html += '<input type="button" class="revertpost" value="回复"/>';
	html += '<input type="hidden" value="'+$(obj).attr("tag")+'"/>';
	html += ' <input type="button" class="revertcancel" value="取消"/>';
	html += '</p</div>';
	if($(".revertcancel").val() == "取消"){
		$(".revertcancel").parent().parent().remove();
	}
	$(obj).parent().next().after(html);
	$(obj).parent().next().next().find("textarea").focus();
	$(obj).parent().next().next().find("textarea").val("对<a href='#revert"+$(obj).attr("id")+"'>"+$(obj).prev().prev().text()+"</a>的回复");
	var oFCKeditor = new FCKeditor( "revertcontent","90%","160px","Basic") ; 
	oFCKeditor.BasePath = ROOT+"/Public/Js/FCKeditor/" ; 
	oFCKeditor.ReplaceTextarea() ;
	$('.revertpost').click(function() {
			postRevert(this);
	});
	$('.revertcancel').click(function() {
			postCancel(this);
	});
	return false;
	
}
/*function resetEditor(){
	setContents("revertcontent",document.getElementById("revertcontent").value)
}; 
function saveEditor(){
	document.getElementById("revertcontent").value = getContents("revertcontent");
} */
function postCancel(obj){
	$(obj).parent().parent().remove();
}

function quanren(){
	$('#img').css("cursor","crosshair");
	$('img#img')
			.imgAreaSelect({ 
				selectionOpacity: 0,
				outeropacity:0.6,
				borderColor1:'#fff',
				borderColor2:'#2782D6',
				borderWidth:5,
				disable : false,
				x1:0,
				y1:0,
				x2:150,
				y2:150
			});
	$('#img').click(function(event){
		//getDiv(event.clientX,event.clientY);
		return false;
	});
}

function getDiv(x,y){
	var left = false;
	img_x = parseInt($("#img").offset().left); //图片的左距
	img_y = parseInt($("#img").offset().top);//图片的顶距
	img_w = parseInt($("#img").width());//图片的宽
	img_h = parseInt($("#img").height());//图片的高
	if($("#quanrenDiv").attr("id") == "quanrenDiv"){
		$("#quanrenDiv").remove();
		$("#firendListDiv").remove();
	}
	var st = parseInt(getScroll());
	_x = (parseInt(x)-50);
	if(_x < img_x){
		_x = img_x;
	}
	if(_x > (img_x + img_w - 100) ){
		_x = (img_x + img_w - 100);
		left = true;
	}
	_y = (parseInt(y)-50+st);
	if(_y > (img_y + img_h- 100) ){
		_y = (img_y + img_h- 100);
		
	}
	if(_y < img_y){
		_y = img_y;
	}
	var div_box = document.createElement("DIV");
	div_box.id = "quanrenDiv";
	div_box.style.position ="absolute";
	div_box.style.width = "100px";
	div_box.style.height = "100px";
	div_box.style.border = "3px solid #fff";
	div_box.style.top = _y +"px";
	div_box.style.left =  _x + "px";
	document.body.appendChild(div_box);
	$('#quanrenDiv').css("cursor","crosshair");
	$('#quanrenDiv').click(function(event){
			getDiv(event.clientX,event.clientY);
	});
	var div_box2 = document.createElement("DIV");
	div_box2.id = "firendListDiv";
	div_box2.style.position ="absolute";
	div_box2.style.width = "100px";
	div_box2.style.height = "250px";
	div_box2.style.background = "#f8f8f8";
	div_box2.style.border = "3px solid #fff";
	div_box2.style.top = _y +"px";
	if(left){
		div_box2.style.left = (_x-110) + "px";
	}else{
		div_box2.style.left = (_x+110) + "px";
	}
	var content = '<div id="friendlist">好友列表加载中...</div>';
	div_box2.innerHTML = content;
	document.body.appendChild(div_box2);
	if(userlistcache == ""){
		$.get(APP+"/friend" , {ajax:1} , function(data){
			userlistcache = data;
			checkuser();
		});
	}else{
		checkuser();
	}
}
function finduser(){
	var friendname = $(".friendinput").val();
	$(".friendname").each(function(){
		$(this).css("display","none");
		var text = $(this).text();
		if(text.indexOf(friendname) != -1){
			$(this).css("display", "block");
		}
	});
}
function checkuser(){
	$("#friendlist").html(userlistcache);
	$(".friendinput").bind("click",function(){  
		if("输入名字" == $(this).val()){$(this).val("");};
		interval = setInterval(finduser,10)
	});
	$(".friendinput").bind("blur",function(){ 
		window.clearInterval(interval);
	});
	$(".friendname").each(function(){
		$(this).click(function(){
			var value = $('#taglist').html();
			value += '<span class="nametag" tag="'+_x+','+_y+'"><a href="'+APP+'/home/'+$(this).attr("id")+'">'+$(this).text()+'</a></span> ';
			$('#taglist').html(value);
			$.get(APP+"/photo/savetag" , {ajax:1 , pid:$("#photoid").val() , taguserid:$(this).attr("id") ,x:(_x-img_x) , y:(_y-img_y)});
			$("#quanrenDiv").remove();
			$("#firendListDiv").remove();
			$('.nametag').each(function(){
				$(this).bind("mouseover",function() {
					if($("#lookDiv").attr("id") == "lookDiv"){
						$("#lookDiv").remove();
					}
					var div_box3 = document.createElement("DIV");
					div_box3.id = "lookDiv";
					div_box3.style.position ="absolute";
					div_box3.style.width = "100px";
					div_box3.style.height = "100px";
					div_box3.style.border = "3px solid #fff";
					div_box3.style.top = _y +"px";
					div_box3.style.left = _x + "px";
					document.body.appendChild(div_box3);
				});
	
				$(this).bind("mouseout",function() {
					$("#lookDiv").remove();
				});
			});
		});
	});
}

function getScroll(o)
{/*获得当前网页的滚动条距离[o=null|parentObj]({left,top})*/
    o=o||null;
    if(o!=null && typeof o=='object')
    {
        var w=o.window;
        var d=o.document;
    }
    else
    {
        var w=window;
        var d=document;
    }
    var dd=d.documentElement;
    var L=0,T=0;
    L=(w.pageXOffset) ? w.pageXOffset : ((dd&&dd.scrollLeft) ? dd.scrollLeft : (d.body ? d.body.scrollLeft : 0));
    T=(w.pageYOffset) ? w.pageYOffset : ((dd&&dd.scrollTop) ? dd.scrollTop : (d.body ? d.body.scrollTop : 0));
    w=d=dd=null;
    return T;
}